/*
 * @lc app=leetcode id=24 lang=cpp
 *
 * [24] Swap Nodes in Pairs
 */
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (!head) {
            return head;
        }

        ListNode *fakeHead = new ListNode(-1);
        fakeHead->next = head;

        ListNode *prev = fakeHead;
        ListNode *node = fakeHead->next;
        while(node&&node->next) {
            ListNode *next = node->next->next;
            prev->next = node->next;
            prev->next->next = node;
            node->next = next;

            prev = node;
            node = next;
        }

        node = fakeHead->next;
        delete fakeHead;
        return node;
    }
};

